<?php
abstract class ArticleActiveRecord extends CActiveRecord{

    protected function purify($text)
    {
        $p = new CHtmlPurifier;
        $p->options = array(
            'HTML.SafeObject'=>true,
            'Output.FlashCompat'=>true,
        );
        return $p->purify($text);
    }

    protected function beforeSave()
    {
        if(parent::beforeSave())
        {
                $this->title = $this->purify($this->title);
                $this->content = $this->purify($this->content);
                $this->description = $this->purify($this->description);
                $this->tags = $this->purify($this->tags);

            return true;
        }
        else
            return false;
    }

    public function beforeDelete()
    {
            $file_path = Yii::getPathOfAlias('webroot').'/images/article/'.$this->id.'/'.$this->image;
            if(file_exists($file_path))
            {
                @unlink($file_path);
                @rmdir(Yii::getPathOfAlias('webroot').'/images/article/'.$this->id);
            }
        return parent::beforeDelete();
    }

    public function behaviors()
    {

        return array(
            'CTimestampBehavior' => array(
                'class'=>'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'create_time',
                'updateAttribute' => 'update_time',
                'setUpdateOnCreate' => true,
            ),
            'URecordBehavior'=>array(
                'class'=>'application.components.URecordBehavior',
                'createUserAttribute'=>'create_user_id',
                'updateUserAttribute'=>'update_user_id',

            )
        );
    }
}